package com.md.common.utils.file;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.FileNameMap;
import java.net.URLConnection;

/**
 * 功能描述：文件工具
 *
 * @author lujianping@oak168.com
 * @version 1.0
 * @date 2018-07-04 14:22
 * @since JDK 1.8
 */
public class FileUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileUtil.class);

    /**
     * Get the Mime Type from a File
     * <p>
     * 参考：http://www.rgagnon.com/javadetails/java-0487.html，http://dada89007.iteye.com/blog/1392606
     * </p>
     *
     * @param fileName
     * @return
     */
    public static String getMimeType(String fileName) {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String type = fileNameMap.getContentTypeFor(fileName);
        LOGGER.debug("type:" + type);
        return type;
    }

    /**
     * 获得指定文件的byte数组
     */
    public static byte[] getBytes(String filePath) {
        byte[] buffer = null;
        File file = new File(filePath);
        buffer = getBytes(file);
        return buffer;
    }

    /**
     * 获得指定文件的byte数组
     */
    public static byte[] getBytes(File file) {
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            buffer = new byte[fis.available()];
            long length = fis.read(buffer);
            fis.close();
            LOGGER.debug("length:" + length);
        } catch (FileNotFoundException e) {
            LOGGER.error("FileNotFoundException异常", e);
        } catch (IOException e) {
            LOGGER.error("IOException异常", e);
        }
        return buffer;
    }

}
